package com.ideal.utils;

import com.ideal.core.constant.Constant;


/**
 * @author
 */
public class StringUtils {
    private StringUtils(){}

    /**
     * 格式化列名(首字母小写)
     * @param columnName
     * @return
     */
    public static String columnNameFormat(String columnName){
        StringBuilder sb = new StringBuilder();
        String [] words = columnName.split("_");
        for(int i=0;i<words.length;i++){
            if(i==0){
                sb.append(words[i].toLowerCase());
            }else{
                sb.append(firstToUpperCase(words[i]));
            }
        }
        return sb.toString();
    }

    /**
     * 格式化列名(首字母大写)
     * @param columnName
     * @return
     */
    public static String columnNameUpperCaseFormat(String columnName){
        StringBuilder sb = new StringBuilder();
        String [] words = columnName.split("_");
        for(int i=0;i<words.length;i++){
            sb.append(StringUtils.firstToUpperCase(words[i]));
        }
        return sb.toString();
    }

    /**
     * 格式化表名(首字母大写)
     * @param tableName
     * @return
     */
    public static String tableNameFormat(String tableName){
        String[] words = tableName.split("_");
        return firstToUpperCase(words[words.length-1]);
    }

    /**
     * 获得列类名的全限定名
     * @param columnClassName
     * @return
     */
    public static String getFullColumnClassName(String columnClassName){
        return typeChange(columnClassName);
    }

    /**
     * 获取列的类名
     * @param columnClassName
     * @return
     */
    public static String getSimpleColumnClassName(String columnClassName){
        columnClassName = typeChange(columnClassName);
        String[] packages  = columnClassName.split("\\.");
        return packages[packages.length-1];
    }

    /**
     * 首字母大写
     * @param word
     * @return
     */
    public static String firstToUpperCase(String word){
        word = word.toLowerCase();
        word = word.substring(0,1).toUpperCase()+word.substring(1,word.length());
        return word;
    }

    /**
     * 首字母小写
     * @param word
     * @return
     */
    public static String firstToLowerCase(String word){
        word = word.substring(0,1).toLowerCase()+word.substring(1,word.length());
        return word;
    }
    /**
     * oracle 数据类型转换为实体类类型
     * @param type
     * @return
     */
    private static String typeChange(String type){
        if(type.toUpperCase().contains("VARCHAR2")){
            return Constant.STRING;
        }else if(type.toUpperCase().contains("TIMESTAMP")) {
            return Constant.DATE;
        }
        return Constant.OBJECT;
    }

    /**
     * @param path
     */
    public static String packageName(String path,String moduleName) {
        path = path.split("src")[path.split("src").length-1];
        path = path.replace("/",".");
        return path.substring(1,path.length())+moduleName.toLowerCase()+Constant.DOT;
    }

    /**
     * 获得当前文件所在的文件夹路径
     * @param path
     * @param fileName
     * @return
     */
    public static String getDirPath(String path,String fileName){
        return path.substring(0,path.length()-fileName.length());
    }

}
